#!/usr/bin/env bash

echo current pid is $$

set -x
set -e

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd ${SCRIPT_DIR}

. /etc/os-release
#import common function
source ${SCRIPT_DIR}/template.sh
source ${SCRIPT_DIR}/utils.sh
source ${SCRIPT_DIR}/set_cluster_common.sh
source ${SCRIPT_DIR}/install_docker.sh
# source ${SCRIPT_DIR}/hosts.sh

function install_kubelet_kubeadm_kubectl(){
    case "$ID" in
      ubuntu*)
        apt-get install -y --allow-change-held-packages --allow-downgrades kubelet=${KUBE_VERSION}-00 kubeadm=${KUBE_VERSION}-00 kubectl=${KUBE_VERSION}-00
        ;;
      rhel*|centos*)
        yum install -y  kubelet-${KUBE_VERSION}-0.x86_64 kubeadm=${KUBE_VERSION}-0.x86_64 kubectl=${KUBE_VERSION}-0.x86_64
        ;;
    esac
    systemctl enable -f kubelet && sudo systemctl start kubelet
}

# step3 stopKeepalivedHaproxy
function stop_keepalived_haproxy(){
    echo ""
}
# step4 genNodeJoinCommand
function gen_node_join_command(){
    echo ""
}
# step5 installKube_ctl_adm_let
function install_kube_ctl_adm_let(){
    install_kubelet_kubeadm_kubectl
}
# step6 joinK8sNode
function join_k8s_cluster(){
    ${K8S_NODE_JOIN_CMD}
}

function add_k8s_worker_node(){
    # step1 setClusterCommon
    function_decorate set_cluster_common
    # step2 installDocker
    function_decorate install_docker
    # step3 stopKeepalivedHaproxy
    function_decorate stop_keepalived_haproxy
    # step4 genNodeJoinCommand
    function_decorate gen_node_join_command
    # step5 installKube_ctl_adm_let
    function_decorate install_kube_ctl_adm_let
    # step6 joinK8sNode
    function_decorate join_k8s_cluster
}

function main(){
    add_k8s_worker_node
}

main
